﻿using System;
using System.Linq;
using System.Xml.Serialization;

namespace TShooter.TfsWord.TeamFoundation
{
    public class WorkItemFieldData
    {
        [XmlAttribute]
        public string Name { get; set; }

        [XmlAttribute]
        public string ReferenceName { get; set; }

        [XmlAttribute]
        public int Id { get; set; }

        [XmlAttribute]
        public FieldUsages Usage { get; set; }

        [XmlAttribute]
        public bool IsIndexed { get; set; }

        [XmlAttribute]
        public bool IsEditable { get; set; }

        [XmlAttribute]
        public bool IsComputed { get; set; }

        [XmlAttribute]
        public string HelpText { get; set; }

        [XmlAttribute]
        public FieldType FieldType { get; set; }

        [XmlAttribute]
        public ReportingType ReportingType { get; set; }

        [XmlAttribute]
        public bool IsQueryable { get; set; }

        [XmlAttribute]
        public bool CanSortBy { get; set; }

        [XmlArray("AllowedValues")]
        [XmlArrayItem("AllowedValue", typeof(string))]
        public AllowedValuesCollection AllowedValues { get; set; }

        [XmlElement]
        public object Value { get; set; }

        [XmlIgnore]
        public Type SystemType
        {
            get
            {
                switch (FieldType)
                {
                    case FieldType.Boolean:
                        return typeof(bool);
                    case FieldType.DateTime:
                        return typeof(DateTime);
                    case FieldType.Double:
                        return typeof (Double);
                    case FieldType.Guid:
                        return typeof (Guid);
                    case FieldType.Integer:
                        return typeof (int);
                    default:
                        return typeof(string);
                }
            }
        }
    }

    public class WorkItemFieldDataCollection: ObservableCollection<WorkItemFieldData>
    {
        public WorkItemFieldData this[string referenceName]
        {
            get
            {
                return this.First(i => i.ReferenceName == referenceName);
            }
        }
    }
}
